var app = getApp();
Page({
    data: {
        imgUrls: [],
        area: [],
        pre_show: "",
        details: "",
        maskVisual: "hidden",
        args_maskVisual: "hidden",
        shop_cart_num: "0",
        selectPayType: "",
        member_id: "",
        front: {
            "0": "全款支付",
            "1": "订金支付"
        },
        hei: '',
        ov: "",
        check_login_flag: "",
        select_type_version_index: 0,
        name: "",
        phone_number: "",
        area: "",
        address: "",
        flag: true,

    },
    /*** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        var that = this;
        var id = options.id;
        this.goodsDetails(id)
        this.setData({
            goods_id: id
        })
        wx.getStorage({
            key: 'miniprogram',
            success: function (res) {
                if (res.data) {
                    wx.showToast({
                        title: '智能数据添加成功',
                        icon: 'success',
                        duration: 2000

                    })
                }
            }
        })
        wx.removeStorage({
            key: 'miniprogram',
            success: function (res) { },
        })
    },

    /** * 生命周期函数--监听页面初次渲染完成*/
    onReady: function () {
      this.wxStatus();
    },
    /** * 生命周期函数--监听页面显示*/
    onShow: function () {
        this.init();
        this.check_login();
    },
    /*** 生命周期函数--监听页面隐藏 */
    onHide: function () {

    },

    /*** 生命周期函数--监听页面卸载*/
    onUnload: function () {

    },

    /*** 页面相关事件处理函数--监听用户下拉动作*/
    onPullDownRefresh: function () {

    },

    /** * 页面上拉触底事件的处理函数*/
    onReachBottom: function () {

    },
    bindPickerChange: function (e) {
        var that = this;
        var index = e.detail.value;
        var area_data = that.data.area[index];
        that.setData({
            index: index,
            area_data: area_data
        })
    },
    preBtn: function () {
        this.setData({
            pre_show: true
        })
        this.area_list();
    },//判断是否登陆
    check_login: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var id = res.data.id;
                if (id) {
                    that.setData({
                        check_login_flag: true
                    })
                } else {
                    that.setData({
                        check_login_flag: false
                    })
                }
            },
            fail: function (res) {
                that.setData({
                    check_login_flag: false
                })
            }
        })
    },
    preHidden: function () {
        this.setData({
            pre_show: ""
        })
    },
    get_name: function (e) {
        var that = this;
        var name = e.detail.value;
        that.setData({
            name: name
        })
    },
    get_phone_number: function (e) {
        var that = this;
        var phone_number = e.detail.value;
        that.setData({
            phone_number: phone_number
        })
    },
    get_address: function (e) {
        var that = this;
        var address = e.detail.value;
        that.setData({
            address: address
        })
    },
    pre_submit: function () {
        var that = this;
        var params = {};
        params['name'] = that.data.name;
        params['phone_number'] = that.data.phone_number;
        params['area'] = that.data.area_data;
        params['address'] = that.data.address;
        params['resource'] = "商城小程序";
        wx.request({
            url: app.config() + 'measure/appointMeasure',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    wx.showToast({
                        title: '提交成功',
                        icon: 'success',
                        duration: 10000
                    })
                } else {
                    wx.showLoading({
                        title: res.data.message,
                        icon: "../../images/warning.png"
                    })
                }
            }
        })
        this.preHidden();
    },//商品详情
    goodsDetails: function (id) {
        var params = {};
        params['id'] = id
        var that = this;
        wx.request({
            url: app.config() + 'goods/details',
            data: params,
            success: function (res) {
                var imgUrl = res.data.data.urls;
                var goods_type = res.data.data.type;//商品类型
                var goods_name=res.data.data.name;
                var delivery_duration = res.data.data.delivery_duration;
                if (goods_type == "衬衣") {
                    res.data.data.delivery_duration = "这件衬衣定制需要生产时间为" + delivery_duration;
                } else if (goods_type == "西装") {
                    res.data.data.delivery_duration = "定制西装需要生产时间" + res.data.data.label82;
                } else if (goods_type == "大衣") {
                    res.data.data.delivery_duration = "定制大衣需要生产时间" + res.data.data.label83;
                } else if (goods_type == "配饰") {
                    res.data.data.delivery_duration = "配饰均为现货商品";
                }
                var tempArr = new Array();
                var longArr = new Array();
                for (var i = 0; i < imgUrl.length; i++) {
                    if (imgUrl[i].file_type == 5) {
                        tempArr.push(imgUrl[i].file_url)
                    } else if (imgUrl[i].file_type == 1) {
                        longArr.push(imgUrl[i].file_url)
                    }
                }
                that.setData({
                    details: res.data.data,
                    imgUrls: tempArr,
                    longUrls: longArr,
                    front_pay: that.data.front[res.data.data.front],
                    goods_type: goods_type,
                    goods_name: goods_name
                })
            }
        })
    },
    cascadeToggle: function () {        //切换购物车开与关
        var that = this;
        that.check_login();
        if (!that.data.check_login_flag) {
            // that.setData({flag: false});
          this.wxStatus();
            return;
        }
        if (that.data.maskVisual == 'show') {
            that.cascadePopup();
            that.setData({
                hei: "90%",
                ov: "hidden"
            })
        } else {
            that.cascadeDismiss();
            that.setData({
                hei: "100%",
                ov: "auto"
            })
        }
    },
    cascadePopup: function () {    // 购物车打开动画
        var that = this;
        var animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease-out'
        });
        this.animation = animation;
        animation.translateY(0).step();
        that.setData({
            animationData: this.animation.export(),
            maskVisual: 'hidden'
        });
    },
    cascadeDismiss: function () {        // 购物车关闭动画
        var that = this;
        var animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease-out'
        });
        this.animation = animation;
        this.animation.translateY(-320).step();
        that.setData({
            animationData: that.animation.export(),
            maskVisual: 'show'
        });
    },//版型名称
    version_name: function (member_id) {
        var that = this;
        var params = {};
        params['member_id'] = member_id;
        wx.request({
            url: app.config() + 'cart/getVersionType',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var data = res.data.data;
                var varsion_title;
                setTimeout(function () {
                    var goods_type = that.data.goods_type;
                    var type_version;
                    if (goods_type == "衬衣") {
                        if (res.data.status == 0) {
                            type_version = data["type_version"];
                            varsion_title = data["type_version"][0];
                        }
                    } else if (goods_type == "西装") {
                        type_version = ["预约来店量体"];
                        varsion_title = "预约来店量体"
                    } else if (goods_type == "大衣") {
                        type_version = ["预约来店量体"];
                        varsion_title = "预约来店量体"
                    } else if (goods_type == "配饰") {
                        type_version = ["默认尺寸"];
                        varsion_title = "默认尺寸"
                    }
                    var ling = type_version[0].indexOf("*");
                    that.setData({
                        version_list: type_version,
                        version_type: data["type"],
                        varsion_title: varsion_title,
                        ling: ling
                    })
                }, 500)
            }
        })
    },//选择版型
    select_version: function (e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var title = e.currentTarget.dataset.title;
        var name = e.currentTarget.dataset.name;
        that.setData({
            varsion_title: title,
            version_t: name,
            select_type_version_index: index
        })
    },//添加购物车
    add_cart: function () {
        // 添加购物车之前判断用户是否登陆
        var that = this;
        var params = {};
        params['member_id'] = that.data.member_id;;
        params['goods_id'] = that.data.goods_id;
        params['goods_number'] = 1;
        var version_t = that.data.version_t;
        params['type_version'] = that.data.varsion_title;
        params['front_pay'] = that.data.front_pay;
        wx.request({
            url: app.config() + 'cart/add',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    wx.showToast({
                        title: '加入购物车成功！',
                        icon: 'success',
                        duration: 500
                    })
                    wx.removeStorage({
                        key: 'miniprogram',
                        success: function (res) {

                        }
                    })
                    that.cascadeToggle();
                    var member_id = that.data.member_id;
                    if (member_id) {
                        that.get_shop_cart_num(member_id);
                    }
                }
            },
            complete: function () {

            }
        })
    },//获取购物车商品个数
    get_shop_cart_num: function (member_id) {
        var that = this;
        var params = {};
        params['member_id'] = member_id;
        wx.request({
            url: app.config() + 'cart/selects',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var shop_cart_num = res.data.data.length;
                that.setData({
                    shop_cart_num: shop_cart_num
                })
            }
        })
    },
    onShareAppMessage: function (res) {
        var that = this;
        var goods_name = that.data.goods_name;
        var goods_id = that.data.goods_id;
        if (res.from === 'button') {
            // 来自页面内转发按钮
            // console.log(res.target)
        }
        return {
            title: goods_name,
            path: '/pages/productInfo/productInfo?id=' + goods_id,
            imageUrl: that.data.imgUrls[0],
            success: function (res) {
                // 转发成功
                // console.log("转发成功")
                that.share_add_point();
            },
            fail: function (res) {
                // 转发失败
                // console.log("转发失败")
            }
        }
    },//查看商品参数
    view_goods_args: function () {
        var that = this;
        if (that.data.args_maskVisual == 'show') {
            that.argsDismiss();
            that.setData({
                hei: "90%",
                ov: "auto"
            })
        } else {
            that.argsPopup();
            that.setData({
                hei: "90%",
                ov: "hidden"
            })
        }
    },
    argsPopup: function () {    // 参数打开动画
        var that = this;
        var animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease-out'
        });
        this.animation = animation;
        animation.translateY(-453).step();
        that.setData({
            argsAnimationData: this.animation.export(),
            args_maskVisual: 'show'
        });
    },
    argsDismiss: function () {        // 参数关闭动画
        var that = this;
        var animation = wx.createAnimation({
            duration: 300,
            timingFunction: 'ease-out'
        });
        this.animation = animation;
        this.animation.translateY(0).step();
        that.setData({
            argsAnimationData: that.animation.export(),
            args_maskVisual: 'hidden'
        });
    },//选择付款方式
    select_pay_type: function (e) {
        var index = e.target.dataset.index;
        var name = e.target.dataset.name;
        this.setData({
            selectPayType: name
        })
    },
    createAi: function () {
        var goods_id = this.data.goods_id;
        var url = '../intelligence/intelligence?id=' + goods_id;
        wx.navigateTo({
            url: url
        })
    },
    init: function () {
        var that = this;
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                if (member_id) {
                    that.get_shop_cart_num(member_id);
                    that.version_name(member_id);
                    that.setData({
                        member_id: member_id
                    })
                }
            }
        })
    },
    area_list: function () {
        var that = this;
        var params = {};
        wx.request({
            url: app.config() + 'index/areaList',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                var area_data = res.data.data;
                var area = new Array();
                for (var i = 0; i < area_data.length; i++) {
                    area.push(area_data[i].location)
                }
                that.setData({
                    area: area
                })
            }
        })
    },//分享加积分
    share_add_point: function () {
        var that = this;
        var params = {};
        params['member_id'] = that.data.member_id;
        wx.request({
            url: app.config() + 'activitycoupon/shareGoods',
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.status == 0) {
                    wx.showModal({
                        title: '分享成功',
                        content: '恭喜您获得10积分',
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }
            }
        })
    },//提交用户信息
    submit_user_info: function (args) {
        wx.request({
            url: app.config() + "index/xiaoWxInfo",
            data: args,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {

            }
        })
    },
  myevent(e) {
    this.setData({
      flag: e.detail.flag,
    })
    this.init();
    this.check_login();
  },
  wxStatus() {
    wx.getSetting({
      success: (res) => {
        if (typeof res.authSetting['scope.userInfo'] === 'undefined' || !res.authSetting['scope.userInfo']) {
          this.setData({
            flag: false,
          })
        } else {
          // this.wxLogin();
          wx.getStorage({
            key: 'memberInfo',
            success: function(res) {
              var id = res.data.id;
              if (!id) {
                wx.switchTab({
                  url: '/pages/myPage/myPage',
                })
              }
            },
          })
        }
        /*
          * res.authSetting = {
          *   "scope.userInfo": true,
          *   "scope.userLocation": true
          * }
          */
      }
    })
  }
})